<template>
  <div class="home-container">
    <div class="home-wrapper">
      <van-swipe
        lazy-render
        class="my-swipe"
        :autoplay="3000"
        indicator-color="white"
      >
        <van-swipe-item>
          <img src="@/assets/banner1.jpg" alt="轮播图" class="banner-image" />
        </van-swipe-item>
        <van-swipe-item>
          <img src="@/assets/banner2.jpg" alt="轮播图" class="banner-image" />
        </van-swipe-item>
        <van-swipe-item>
          <img src="@/assets/banner3.jpg" alt="轮播图" class="banner-image" />
        </van-swipe-item>
      </van-swipe>
      <div class="home-content-container">
        <div class="home-content-title">重要通知</div>
        <div class="home-content">
          <img src="@/assets/report.png" alt="welcome" />
          <div style="display: flex; flex-direction: column">
            <div
              class="my_button_1"
              style="margin-top: -40px"
              @click="navigateTo('/test/analysis')"
            >
              置顶
            </div>
            <span
              @click="navigateTo('/test/echarts/analysis')"
              style="margin-left: 70px; margin-top: -22px; cursor: pointer"
              >查看详情</span
            >
          </div>
          <div style="margin-top: 65px; margin-left: -125px; cursor: default">
            <span>
              欢迎用户: <span class="username">{{ name }}</span>
            </span>
            <span style="display: block" v-if="showRank">
              您在班级的排名为：第<span class="rank">{{ rank }}</span
              >名
            </span>
          </div>
        </div>
        <div
          style="
            width: 95%;
            height: 1px;
            background-color: rgb(237, 226, 226);
            margin: 0 auto;
          "
        ></div>
        <div class="home-content">
          <img src="@/assets/test.png" alt="welcome" />
          <div style="display: flex; flex-direction: column">
            <div
              @click="navigateTo('/test/index')"
              class="my_button_1"
              style="margin-top: -40px"
            >
              置顶
            </div>
            <span
              @click="navigateTo('/test/index')"
              style="margin-left: 70px; margin-top: -22px; cursor: pointer"
              >查看试卷</span
            >
          </div>
          <div style="margin-top: 65px; margin-left: -125px; cursor: default">
            <span>
              您还有<span style="color: red">{{
                store.getters.paperCount
              }}</span
              >份试卷未做
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from "vue";

import { useStore } from "vuex";
import gradeApi from "@/api/grade";
import router from "@/router";

const store = useStore();
const name = computed(() => {
  return store.getters.name;
});
const showRank = ref(false);
const rank = ref(0);

const carouselItems = ref([
  {
    id: 1,
    title: "轮播图1",
    imgUrl: new URL("@/assets/banner1.jpg", import.meta.url).href,
  },
  {
    id: 2,
    title: "轮播图2",
    imgUrl: new URL("@/assets/banner2.jpg", import.meta.url).href,
  },
  {
    id: 3,
    title: "轮播图3",
    imgUrl: new URL("@/assets/banner3.jpg", import.meta.url).href,
  },
]);

const navigateTo = (path) => {
  router.push(path);
};

const getRank = async function () {
  const res = await gradeApi.getUserRank();
  rank.value = res.data;
  if (rank.value > 0) {
    showRank.value = true;
  }
};

onMounted(() => {
  if (store.getters.roles.includes("student")) {
    getRank();
  }
});
</script>

<style scoped>
.home-container {
  overflow: hidden;
  overscroll-behavior: none; /* 阻止下拉刷新 */
  touch-action: none; /* 禁用所有默认的触摸行为 */
  position: fixed; /* 防止 iOS 橡皮筋效果 */
  height: 100vh;
  width: 100%;
  -webkit-overflow-scrolling: touch;
}

/* 添加一个内容包装器 */
.home-wrapper {
  min-height: 100%;
  padding-bottom: env(safe-area-inset-bottom); /* 适配底部安全区域 */
}

/* 当内容滚动到底部时防止继续上拉 */
.home-container::-webkit-scrollbar {
  display: none; /* 隐藏滚动条 */
}

.my-swipe .van-swipe-item {
  color: #fff;
  font-size: 20px;
  line-height: 150px;
  text-align: center;
  background-color: #39a9ed;
}

.banner-image {
  width: 100%;
  height: auto;
  display: block;
}

.home-content-container {
  border: 1px solid rgb(237, 226, 226);
  border-radius: 3px;
  margin: 0 auto;
  margin-top: 20px;
  width: 95%;
}

.home-content {
  flex-direction: row;
  display: flex;
  justify-content: left;
  align-items: center;
  padding: 10px;
}

.home-content-title {
  font-size: 18px;
  display: flex;
  background-color: rgb(217, 162, 135);
  justify-content: center;
  align-items: center;
  width: 140px;
  height: 40px;
  color: white;
  border-radius: 5px;
  margin-left: -20px;
  margin-top: 10px;
}

.table-content {
  width: 100%;
  border: 1px solid rgb(237, 226, 226);
  border-radius: 5px;
  margin: 0 auto;
  margin-top: 20px;
}

.username {
  color: rgb(26, 189, 26);
}

.rank {
  color: rgb(255, 0, 0);
}

.my_button_1 {
  width: 50px;
  height: 20px;
  border-radius: 5px;
  color: white;
  text-align: center;
  line-height: 20px;
  font-size: 12px;
  cursor: pointer;
  background-color: rgb(64, 158, 255);
  margin-left: 10px;
}

.home-content img {
  height: 100px;
  width: auto; /* 让宽度自动按比例缩放 */
  object-fit: contain; /* 确保图片按比例缩放且完整显示 */
}
</style>
